function h5linkput(h5file,target_path,link_path,link_name,link_type)
%H5LINKPUT  Write HDF5 link.
%
%   H5LINKPUT(FILE,TARGET_PATH,LINKPATH,LINKNAME,LINK_TYPE)
%   creates/overwrites a link at the path linkpath with name link_name to
%   target_path. For soft links, TARGET_PATH can be a relative path. FILE
%   must be an H5ML.id or a file name.  LINK_TYPE can be either 'soft'
%   (default) or 'hard'.
%
%   Example:  Create a dataset '/g1/DS1', a group '/g2', then create a soft 
%   link'/g2/DS2' that points back to '/g1/DS1'.
%       h5filecreate('myfile.h5');
%       h5datacreate('myfile.h5','/g1/DS1','size',[10 20]);
%       h5groupput('myfile.h5','/g2');
%       h5linkput('myfile.h5','/g1/DS1','/g2','DS2','soft');
%       h5dump('myfile.h5');
%
%   Example:  Create a dataset '/g1/DS1', a group '/g2', then create a hard
%   link '/g2/DS2' that points back to '/g1/DS1'.
%       h5filecreate('myfile.h5');
%       h5datacreate('myfile.h5','/g1/DS1','size',[10 20]);
%       h5groupput('myfile.h5','/g2');
%       h5linkput('myfile.h5','/g1/DS1','/g2','DS2','hard');
%       h5dump('myfile.h5');
%
%   This function requires R2009b or higher.

%   Copyright 2010 The MathWorks, Inc.
%   Copyright 2009 Philip Top

v = version('-release');
switch(v)
    case {'2007a', '2007b', '2008a', '2008b','2009a'}
        error('HDF5TOOLS:H5LINKPUT:notAvailable', ...
            'H5LINKPUT requires 2009b or higher to work.');
end

if (nargin<5)
    link_type='soft';
end

if ~ischar(target_path)
    error('MATLAB:H5LINKPUT:badDatatype', ...
        'target_path input argument must have datatype char.' );
end

if ~ischar(link_path)
    error('MATLAB:H5LINKPUT:badDatatype', ...
        'link_path input argument must have datatype char.' );
end

if ~ischar(link_name)
    error('MATLAB:H5LINKPUT:badDatatype', ...
        'link_name input argument must have datatype char.' );
end

switch(link_type)
    case {'hard','soft','HARD','Hard','SOFT','Soft','external',}
    otherwise
        error('MATLAB:H5LINKPUT:badLinktype', ...
            'LINK type input argument must be ''hard'' or ''soft''.' );
end


flags = 'H5F_ACC_RDWR';
plist_id = 'H5P_DEFAULT';

if isa(h5file,'H5ML.id')
    file_id = h5file;
    close_file = false;
else
    file_id     = H5F.open(h5file,flags,plist_id);
    close_file = true;
end

try
    parent_id=H5G.open (file_id, link_path );
catch 
    %error if the parent object is a dataset
    error('MATLAB:H5LINKPUT:invalid_parent', ...
        'LINK location must be a the root directory or a group' );
end

lcpl_id=H5P.create('H5P_LINK_CREATE');
lapl_id=H5P.create('H5P_LINK_ACCESS');

if (H5L.exists(parent_id,link_name,lapl_id))
    return;
end

switch(link_type)
    case {'soft','Soft','SOFT'}
        %create a soft link
        
        H5L.create_soft(target_path,parent_id,link_name,lcpl_id,lapl_id);
    case {'hard','Hard','HARD'}
        
        H5L.create_hard(parent_id,target_path,parent_id,link_name,lcpl_id,lapl_id);
        
    case {'external','External','EXTERNAL'}
        
    otherwise
        
        if parent_obj_type==0
            H5G.close(parent_id);
        end
        
        if ischar(h5file)
            H5F.close(file_id);
        end
        error('MATLAB:H5LINKPUT:invalid_link_type', ...
            'LINK type must be either hard or soft' );
end

H5G.close(parent_id);


if close_file
    H5F.close(file_id);
end



